package com.example.safe.keyboard;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.safe.keyboard.databinding.ActivityCryptTestBinding;
import com.surfin.safe.keyboard.SafeKeyboard;
import com.surfin.safe.keyboard.SafeKeyboardConfig;

public class CryptTestActivity extends AppCompatActivity {

    private SafeKeyboard safeKeyboard;

    private ActivityCryptTestBinding binding;

    public static void start(Context context) {
        context.startActivity(new Intent(context, CryptTestActivity.class));
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityCryptTestBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        setTitle(R.string.crypt_test);
        SafeKeyboardConfig config = SafeKeyboardConfig.getDefaultConfig();
        // 加密显示
        config.encryptedDisplay = true;
        safeKeyboard = new SafeKeyboard(this, binding.llContainer, binding.ctlRoot, config);
        safeKeyboard.putEditText(binding.etText);
        binding.etNumber.setTag("number");
        safeKeyboard.putRandomEdit(binding.etNumber);
        binding.btnGetText.setOnClickListener(v -> {
            // 显示EditText的数据
            binding.tvText.setText(binding.etText.getText());
        });
        binding.btnGetDecryptText.setOnClickListener(v -> {
            // 显示解密后的数据
            binding.tvText.setText(binding.etText.getEditString());
        });
        // ---------------------------------------------数字
        binding.btnGetNumber.setOnClickListener(v -> {
            // 显示EditText的数据
            binding.tvNumber.setText(binding.etNumber.getText());
        });
        binding.btnGetDecryptNumber.setOnClickListener(v -> {
            // 显示解密后的数据
            binding.tvNumber.setText(binding.etNumber.getEditString());
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (safeKeyboard != null) {
            safeKeyboard.release();
            safeKeyboard = null;
        }
    }
}
